package com.victor.controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Enterbrandmation;
import com.victor.entity.Shopuser;
import com.victor.service.EnterbrandmationService;
import com.victor.utils.DateUtils;
import com.victor.utils.Msg;

/**
 * 入驻审批
 */
@RestController
@RequestMapping("/enterbrandmation")
public class EnterbrandmationController {
	
	@Autowired
	private EnterbrandmationService enterbrandmationService;
	/**
	 * 	根据入驻审批状态值查询列表
	 * 
	 * status为0查询新的申请列表
	 * status为1，查询审批通过列表
	 * status为2，查询审批拒绝列表
	 */
	@RequestMapping("/selectList")
	public Msg selectList(String status,String starttime, String endtime){
		List<Enterbrandmation> list = enterbrandmationService.selectAll(status,starttime,endtime);
		return Msg.success().add("list", list); 
	}
	
	
	
	/**
	 * 根据Id查看详情页信息
	 */
	@RequestMapping("/getMessage")
	public Msg getMessage(Integer id) {
		Enterbrandmation ebm = enterbrandmationService.selectById(id);
		if (ebm != null ) {
			return Msg.success().add("ebm", ebm);
		}
		return Msg.fail();	
	}
	
	
	/**
	 *	 审批通过
	 * @return
	 */
	@RequestMapping("/pass")
	public Msg pass(Integer id) {
		boolean a = enterbrandmationService.pass(id);
		if (!a) {
			return Msg.fail();
		}
		return Msg.success();
	}
	
	/**
	 * 审批拒绝
	 * @return
	 */
	@RequestMapping("/noPass")
	public Msg NoPass(Integer id) {
		boolean b = enterbrandmationService.noPass(id);
		if (!b) {
			return Msg.fail();
		}
		return Msg.success();
	}
	

	/**
	 * 根据审批状态和起止时间批量导出到表格
	 */
		@RequestMapping("/batchExport")
		public void batchExport(String status,String starttime, String endtime ,HttpServletRequest request,HttpServletResponse response) throws Exception {
			//数据库查询数据 exportlist 需要导出的list
			List<Enterbrandmation> enterbrandmationList = enterbrandmationService.selectAll(status,starttime,endtime);
			// 创建poi导出数据对象
			SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
			// 创建sheet页
			Sheet sheet = sxssfWorkbook.createSheet("入驻列表");

			CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 8);
			// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
			sheet.addMergedRegion(region1);
			Row headTitle = sheet.createRow(0);
			headTitle.createCell(0).setCellValue("入驻列表");

			// 创建表头
			Row headRow = sheet.createRow(9);
			// 设置表头信息
			headRow.createCell(0).setCellValue("入驻编号");
			headRow.createCell(1).setCellValue("登录用户");
			headRow.createCell(2).setCellValue("注册时间");
			headRow.createCell(3).setCellValue("联系人");
			headRow.createCell(4).setCellValue("电话");
			headRow.createCell(5).setCellValue("状态");//0待审批，1已通过，2已拒绝
			headRow.createCell(6).setCellValue("商品名称");
			headRow.createCell(7).setCellValue("商品信息");

			// 遍历上面数据库查到的数据
			for (Enterbrandmation su : enterbrandmationList) {
				Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(su.getId());
				dataRow.createCell(1).setCellValue(su.getUsername());
				dataRow.createCell(2).setCellValue(su.getDate());
				dataRow.createCell(3).setCellValue(su.getLinkman());
				dataRow.createCell(4).setCellValue(su.getPhone());
				dataRow.createCell(5).setCellValue(su.getStatus().equals(0) ? "待审批" : (su.getStatus().equals(1) ? "已通过" : "已拒绝"));
				dataRow.createCell(6).setCellValue(su.getMerch_name());
				dataRow.createCell(7).setCellValue(su.getMerch_message());
			}
			// 下载导出文件名
			String filename = "入驻审批列表";
			// 设置头信息
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/vnd.ms-excel");
			// 一定要设置成xlsx格式
			response.setHeader("Content-Disposition",
					"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
			// 创建一个输出流
			ServletOutputStream outputStream = response.getOutputStream();
			// 写入数据
			sxssfWorkbook.write(outputStream);
			// 关闭
			outputStream.close();
			sxssfWorkbook.close();
		}

	
		/**
		 *  批量删除入驻商家
		 */
	@RequestMapping("/batchDelete")
	public Msg batchDelete(int[] ids) {
		boolean b = enterbrandmationService.batchDelete(ids);
		if (b) {
			return Msg.success();
		}
		return Msg.fail();
	}
	
	/**
	 * 根据联系人姓名商品信息模糊查询入驻商家列表
	 */
	@RequestMapping("/search")
	public Msg likeSearch(String search) {
		List<Enterbrandmation> list = enterbrandmationService.likeSearch(search);
		return Msg.success().add("list", list);
	}
}
